import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login', //重定向
    },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('@/views/login/Login.vue')
    },
    {
        path: '/index',
        name: 'index',
        redirect: '/home',
        meta: { title: '首页' },
        component: () =>
            import ('@/views/index/Index.vue'),
        children: [{
                path: '/home',
                name: 'home',
                meta: { title: '首页' },
                component: () =>
                    import ('@/views/index/home/Home.vue'),
            },
            {
                path: '/product',
                name: 'product',
                meta: { title: '产品列表' },
                component: () =>
                    import ('@/views/index/product/Product.vue')
            },
            {
                path: '/productcategory',
                name: 'productcategory',
                meta: { title: '待审核产品' },
                component: () =>
                    import ('@/views/index/product/ProductCategory.vue')
            },
            {
                path: '/recycleProduct',
                meta: { title: '回收站产品' },
                component: () =>
                    import ('@/views/index/product/RecycleProduct.vue')
            },
            {
                path: '/unauditeproduct',
                name: 'unauditeproduct',
                meta: { title: '产品分类' },
                component: () =>
                    import ('@/views/index/product/UnauditedProduct.vue')
            },
            {
                path: '/user',
                name: 'user',
                meta: { title: '用户列表' },
                component: () =>
                    import ('@/views/index/user/User.vue')
            },
            {
                path: '/system',
                name: 'system',
                meta: { title: '账户管理' },
                component: () =>
                    import ('@/views/index/system/System.vue')
            },
            {
                path: '/shuffl',
                name: 'shuffl',
                meta: { title: '轮播管理' },
                component: () =>
                    import ('@/views/index/shuffl/Shuffl.vue')
            },
            {
                path: '/my',
                name: 'my',
                meta: { title: '个人资料' },
                component: () =>
                    import ('@/views/index/my/My.vue')
            },
            {
                path: '/order',
                name: 'order',
                meta: { title: '订单页面' },
                component: () =>
                    import ('@/views/index/order/Order.vue')
            }
        ]
    },

]

const router = new VueRouter({
    // mode: 'history',
    // base: process.env.BASE_URL,
    routes
})

//路由拦截  在跳转前执行
router.beforeEach(function(to, from, next) {
    if (!sessionStorage.getItem('loginname')) {
        if (to.path != '/login') {
            next('/login')
        }
    }
    next()
})

export default router